import { View } from "../../../../mvc/core/View";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { BaseItem } from "../../common/BaseItem";
import { Language } from "../../../../../extend/language/Language";
import { GNumericStepper } from "../../../../../com/mui/controls/GNumericStepper";
import { ResouceConst } from "../../../../component/gconst/ResouceConst";
import { ShopItemInfo } from "../../../resource/info/ShopItemInfo";
import { ItemData } from "../../../resource/info/ItemData";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { BuyAlert } from "../BuyAlert";
import { Cache } from "../../../cache/Cache";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { EBind } from "../../../../../Message/Public/EBind";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { TItem } from "../../../../../Message/Db/Tables/TItem";
import { ColorInfo } from "../../../resource/info/ColorInfo";
type int = number;
//class BuyCommonPanel
    
    export  class BuyCommonPanel extends View
    {
        constructor()
        {
            super();
            this._ingot = new Object /* flash.display.Bitmap */();
            this.init();
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.onPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackChangeHandler);
            return;
        }

        public init(): void
        {
            this._bg = ObjCreate.createBg(0, 0, 260, 70, this);
            ObjCreate.createBg(10, 10, 38, 38, this, ImagesConst.PackItemBg);
            this._propItem = new BaseItem();
            this._propItem.isDragAble = false;
            this._propItem.setSize(32, 32);
            this._propItem.label = "";
            this._propItem.move(12, 12);
            this.addChild(this._propItem);
            this._nameText = ObjCreate.createTextField("", 60, 15, 100, 20, this);
            this._remainAmountText = ObjCreate.createTextField("", 145, 15, 100, 20, this);
            ObjCreate.createTextField(Language.getString(54127), 60, 35, 100, 20, this);
            this._priceNumText = ObjCreate.createTextField("", 95, 35, 35, 20, this);
            this._ingot = new Object /* flash.display.Bitmap */();
            this._ingot.x = 115;
            this._ingot.y = 35;
            this.addChild(this._ingot);
            this._numericStepper = new GNumericStepper();
            this._numericStepper.drawNow();
            this._numericStepper.setSize(50, 20);
            this._numericStepper.move(145, 35);
            this._numericStepper.styleName = "NumericStepper";
            this._numericStepper.drawNow();
            this._numericStepper.textField.setStyle("upSkin", ResouceConst.getScaleBitmap(ImagesConst.WindowCenterB));
            this._numericStepper.textField.drawNow();
            this._numericStepper.maximum = 99;
            this._numericStepper.minimum = 1;
            this.addChild(this._numericStepper);
            this._buyBtn = ObjCreate.createGButton(Language.getString(54128), 205, 30, 48, 24, this);
            this._buyBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBuyBtnClickHandler);
            return;
        }

        public updateByShopItemInfo(arg1: ShopItemInfo): void
        {
            this.shopItemInfo = arg1;
            if (arg1) 
            {
                this._priceUnit = arg1.unit;
                if (arg1.itemInfo && arg1.itemInfo.item) 
                {
                    this.item = this.shopItemInfo.itemInfo.item;
                    this._itemData = new ItemData(this.item.code);
                    this._nameText.htmlText = ItemsUtil.getItemName(this._itemData);
                    this._priceNumText.text = arg1.tshopSell.price.toString();
                    this._ingot.bitmapData = GlobalClass.getBitmapData(ImagesConst.Yuanbao);
                    this._propItem.itemData = this._itemData;
                    this._remainAmountText.x = this._nameText.x + this._nameText.width + 5;
                }
                this.onPackChangeHandler(null);
            }
            else 
            {
                this._nameText.text = "";
                this._propItem.itemData = null;
                this._priceNumText.text = "0";
                this._itemData = null;
                this._propItem.setAmountText(-1);
            }
            return;
        }

        /* internal  */onBuyBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=this._numericStepper.value;
            if (this.shopItemInfo) 
            {
                this.shopItemInfo.buyNum = loc1;
                BuyAlert.showByShopItemInfo(this.shopItemInfo);
            }
            return;
        }

        /* internal  */onPackChangeHandler(arg1: Object=null): void
        {
            var data: Object=null;
            var amount: int;
            var tmpItemData: ItemData;
            var i: int;
            var filterItemData: Function;

            var loc1;
            amount = 0;
            tmpItemData = null;
            i = 0;
            data = arg1;
            if (this._itemData) 
            {
                filterItemData = (arg1: ItemData): Boolean
                {
                    return arg1.category == _itemData.category && arg1.type == _itemData.type && arg1.itemInfo.item.itemLevel == _itemData.itemInfo.item.itemLevel;
                }
                this._itemDataArray = Cache.instance.pack.backPackCache.getItemsByJudge(filterItemData);
                i = 0;
                while (i < this._itemDataArray.length) 
                {
                    tmpItemData = this._itemDataArray[i];
                    amount = amount + tmpItemData.itemAmount;
                    ++i;
                }
                this._propItem.setAmountText(amount);
                this._packItemAmount = amount;
                if (amount != 0) 
                {
                    this._propItem.filters = [];
                }
                else 
                {
                    this._propItem.filters = [FilterConst.colorFilter];
                }
            }
            return;
        }

        public get itemDataArray(): Array<any>
        {
            return this._itemDataArray;
        }

        public set itemDataArray(arg1: Array<any>)
        {
            this._itemDataArray = arg1;
            return;
        }

        public get packItemAmount(): int
        {
            return this._packItemAmount;
        }

        public set packItemAmount(arg1: int)
        {
            this._packItemAmount = arg1;
            return;
        }

        public get itemData(): ItemData
        {
            return this._itemData;
        }

        public getToUseItemData(): ItemData
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._itemDataArray.length) 
            {
                loc1 = this._itemDataArray[loc2];
                if (loc1.itemInfo.item.bind == EBind._EBindYes) 
                {
                    return loc1;
                }
                ++loc2;
            }
            if (this._itemDataArray.length > 0) 
            {
                return this._itemDataArray[0];
            }
            return null;
        }

        public get isShowBg(): Boolean
        {
            return this._isShowBg;
        }

        public set isShowBg(arg1: Boolean)
        {
            this._isShowBg = arg1;
            if (this._isShowBg) 
            {
                this._bg.visible = true;
            }
            else 
            {
                this._bg.visible = false;
            }
            return;
        }

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_nameText: Object /* flash.text.TextField */;

        /* internal */ /* var */_propItem: BaseItem;

        /* internal */ /* var */_remainAmountText: Object /* flash.text.TextField */;

        /* internal */ /* var */_priceNumText: Object /* flash.text.TextField */;

        /* internal */ /* var */_ingot: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_numericStepper: GNumericStepper;

        /* internal */ /* var */_buyBtn: GButton;

        /* internal */ /* var */shopItemInfo: ShopItemInfo;

        /* internal */ /* var */_itemData: ItemData;

        /* internal */ /* var */item: TItem;

        /* internal */ /* var */colorInfo: ColorInfo;

        /* internal */ /* var */colorName: string;

        /* internal */ /* var */_priceUnit: int;

        /* internal */ /* var */_itemDataArray: Array<any>;

        /* internal */ /* var */_packItemAmount: int;

        /* internal */ /* var */_isShowBg: Boolean;
    }
